package com.goktech.project.hmw.service.impl;

import com.goktech.project.hmw.dao.ICityDao;
import com.goktech.project.hmw.mapper.City;
import com.goktech.project.hmw.service.ICityService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityServiceImpl implements ICityService {

	@Autowired
	private StringRedisTemplate redisTemplate;
	@Autowired
	ICityDao iCityDao;

	@Override
	public City getCity(String name) {
		return iCityDao.getCity(name);
	}

	@Override
	public List<City> listCity(String county) {
		return iCityDao.listCity(county);
	}

	@Override
	public List<City> listAllCity() {
		List<City> list=null;
		String county="listAllCity1";
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<City>) JSONArray.toCollection(jsonArray,City.class);
		}else{
			list= iCityDao.listAllCity();
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	@Override
	public List<City> listAllCityContainTotal(){
		List<City> list=null;
		String county="listtotalss";
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<City>) JSONArray.toCollection(jsonArray,City.class);
		}else{
			list= iCityDao.listAllCityContainTotal();
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	/**
	 *
	 * 获取全市发放总金额
	 * @return
	 */
	@Override
	public String total_money(){
		return iCityDao.total_money();
	}

}
